package com.gengzp.system.utils;

import com.gengzp.common.model.generic.entity.GenericEntity;
import com.gengzp.common.model.generic.resp.GenericResp;
import com.gengzp.common.model.user.entity.SysUser;
import com.gengzp.permission.utils.PermissionUtils;
import com.gengzp.system.mapper.SysUserMapper;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName OperatorInfoUtils
 * @Description 操作人信息工具类, 用于在查询数据时填充创建人和修改人信息
 * @Author gengzp
 * @Date 2025/9/19 10:02
 */
@Component
public class OperatorInfoUtils {

    private static SysUserMapper sysUserMapperInstance;

    private OperatorInfoUtils(SysUserMapper sysUserMapper) {
        sysUserMapperInstance = sysUserMapper;
    }

    /**
     * 填充通用返回实体类的操作人信息
     *
     * @param genericResp 通用返回实体类对象
     */
    public static void fillInGenericRespOperatorInformation(GenericResp genericResp) {
        if (Objects.isNull(genericResp)) {
            return;
        }

        // 取出实体类的操作人id
        Set<Long> operatorIds = new HashSet<>(List.of(
                Long.valueOf(genericResp.getCreatedBy()), Long.valueOf(genericResp.getLastModifiedBy())));
        if (CollectionUtils.isEmpty(operatorIds)) {
            return;
        }

        // 循环填充操作人信息
        fillInOperatorNicknameById(buildUserId2NicknameMap(operatorIds), genericResp);
    }

    /**
     * 填充通用返回实体类集合的操作人信息
     *
     * @param genericRespList 通用返回实体类集合
     */
    public static void fillInGenericRespOperatorInformation(List<? extends GenericResp> genericRespList) {
        if (CollectionUtils.isEmpty(genericRespList)) {
            return;
        }

        // 取出实体类操作人id
        Set<Long> operatorIds = genericRespList.stream().map(v ->
                        new HashSet<>(List.of(Long.valueOf(v.getCreatedBy()), Long.valueOf(v.getLastModifiedBy()))))
                .flatMap(Set::stream).collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(operatorIds)) {
            return;
        }

        // 用户id昵称映射 key:用户id    value:用户昵称
        Map<Long, String> userId2NicknameMap = buildUserId2NicknameMap(operatorIds);

        // 循环填充数据集合中的操作人
        genericRespList.forEach(genericResp ->
                fillInOperatorNicknameById(userId2NicknameMap, genericResp));
    }

    /**
     * 组装 userId 对应 nickname 映射
     *
     * @param operatorIds 操作人id集合
     * @return userId 对应 nickname 映射
     */
    private static Map<Long, String> buildUserId2NicknameMap(Collection<Long> operatorIds) {
        Map<Long, String> userId2NicknameMap = Maps.newHashMap();
        // 根据操作id查询用户信息
        List<SysUser> operators = sysUserMapperInstance.selectSysUsersByIds(operatorIds);
        if (CollectionUtils.isNotEmpty(operators)) {
            userId2NicknameMap = operators.stream()
                    .collect(Collectors.toMap(GenericEntity::getId, SysUser::getUserNickname));
        }
        userId2NicknameMap.put(Long.valueOf(PermissionUtils.getSuperAdminUserId()), "超级管理员");
        return userId2NicknameMap;
    }

    /**
     * 根据操作人id填充对应昵称
     *
     * @param userId2NicknameMap userId 对应 nickname 映射
     * @param genericResp        通用返回实体类对象
     */
    private static void fillInOperatorNicknameById(Map<Long, String> userId2NicknameMap, GenericResp genericResp) {
        genericResp.setCreatedByUserNickname(
                userId2NicknameMap.getOrDefault(Long.valueOf(genericResp.getCreatedBy()), "用户已不存在"));
        genericResp.setLastModifiedByUserNickname(
                userId2NicknameMap.getOrDefault(Long.valueOf(genericResp.getLastModifiedBy()), "用户已不存在"));
    }

}
